﻿using Smart.Model;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ActorManager : MonoBehaviour
{
    protected Transform actorParent;
    public static ActorManager Instance { get; private set; }

    Dictionary<long, int> mActorId2AgentId = new Dictionary<long,int>(32);
    Dictionary<long, Actor> mActors = new Dictionary<long, Actor>(32);
    private MainPlayer mainPlayer;
    public MainPlayer MainPlayer => mainPlayer;
    private void Awake()
    {
        Instance = this;
        Log.Info("[角色管理器]:启动成功...");
    }

    public void CreatePlayer(long guid,long x,long y,long z)
    {
        if (mActors.ContainsKey(guid))
        {
            Log.Error($"Actor has created guid = {guid}");
            return;
        }

        var raceMgr = GameEntry.RaceMgr;
        if (null == raceMgr)
        {
            Log.Error($"RaceLogic has not startup yet ...");
            return;
        }

        if (mActorId2AgentId.ContainsKey(guid))
        {
            Log.Error($"Actor has been created ...");
            return;
        }

        int agentId = raceMgr.AddAgent(x, y, z);
        if (agentId == -1)
        {
            Log.Error($"AddAgent failed when to create player ...");
            return;
        }
        mActorId2AgentId.Add(guid, agentId);

        Player player = GameEntry.Instance.Clone<Player>("Player",null);
        if (!player)
        {
            Log.Error($"create player for graphic failed");
            return;
        }
        mActors.Add(guid, player);

        player.AgentId = agentId;
        player.OnCreate();
    }

    public void CreateMainPlayer(long guid, long x, long y, long z)
    {
        if (mActors.ContainsKey(guid))
        {
            Log.Error($"Actor has created guid = {guid}");
            return;
        }

        var raceMgr = GameEntry.RaceMgr;
        if (null == raceMgr)
        {
            Log.Error($"RaceLogic has not startup yet ...");
            return;
        }

        if (mActorId2AgentId.ContainsKey(guid))
        {
            Log.Error($"Actor has been created ...");
            return;
        }

        var agentId = raceMgr.AddAgent(x, y, z);
        
        if (agentId == -1)
        {
            Log.Error($"AddAgent failed when to create player ...");
            return;
        }
        mActorId2AgentId.Add(guid, agentId);

        mainPlayer = GameEntry.Instance.Clone<MainPlayer>("MainPlayer", null);
        if (!mainPlayer)
        {
            Log.Error($"create mainplayer for graphic failed");
            return;
        }
        mActors.Add(guid, mainPlayer);
        mainPlayer.transform.parent = transform;
        mainPlayer.AgentId = agentId;
        mainPlayer.gameObject.SetActive(true);
        mainPlayer.OnCreate();
    }

    public void SyncMainPosition(long x, long y, long z)
    {
        mainPlayer.SyncPosition(x, y, z);
    }

    public void ResetMainPosition(long x, long y, long z)
    {
        mainPlayer.ResetPosition(x, y, z);
    }
    
    public Actor GetActor(long guid)
    {
        if (mActors.ContainsKey(guid))
            return mActors[guid];
        return null;
    }

}